import _ from 'lodash';

import {
    GET_ALL_MENU,
    GET_ALL_MENU_SUCCESS,
    UPDATE_NAVPATH
} from '../actions/menu';

const initialState = {

    selectedKey: ['menu103'],

    currentIndex: 0,
    items: [],
    navpath: []
};

function getCurrentKey(menus, aPath) {
    let cPath = []
    menus.map((item)=> {
        item.child.map((sub)=> {
            //if (aPath.startsWith(sub.link)) { // 判断当前路径是否吻合
            if (aPath==(sub.link)) { // 判断当前路径是否吻合     IE中不支持startsWith，暂时换成“=”
                cPath.push('menu' + sub.key)
            }
        })
    })

    return cPath
}

export default function menu(state = initialState, action = {}) {
    switch (action.type) {


        case GET_ALL_MENU_SUCCESS:

            let menus = action.payload.menus
            let cPath = getCurrentKey(menus,action.meta)
            return Object.assign({}, initialState, {items: menus, selectedKey: cPath});


        case UPDATE_NAVPATH:
            let navpath = [], tmpOb, tmpKey, child;
            if (action.payload.data) {
                action.payload.data.reverse().map((item)=> {
                    if (item.indexOf('sub') != -1) {
                        tmpKey = item.replace('sub', '');
                        tmpOb = _.find(state.items, function (o) {
                            return o.key == tmpKey;
                        });
                        child = tmpOb.child;
                        navpath.push({
                            key: tmpOb.key,
                            name: tmpOb.name
                        })
                    }
                    if (item.indexOf('menu') != -1) {
                        tmpKey = item.replace('menu', '');
                        if (child) {
                            tmpOb = _.find(child, function (o) {
                                return o.key == tmpKey;
                            });
                        }
                        navpath.push({
                            key: tmpOb.key,
                            name: tmpOb.name
                        })
                    }
                })
            }

            let xPath = getCurrentKey(state.items, action.payload.currentPath)

            return Object.assign({}, state, {
                currentIndex: action.payload.key * 1,
                navpath: navpath,
                selectedKey: xPath
            });


        default:
            return state;
    }
}
